//============================================================================
//
// saInfo.js
// ---------
//
// This module exports an object which describes the environment in which
// Fourdit is running. That object contains other objects which in turn hold
// key/value pairs of information. All presentation should be done by the
// CLI or WWW layers.
//
//============================================================================

var os = require('os'),
	config = require('jsonconfig'),
	fs = require('fs'),
	path = require('path');

config.load(['config/config.json']);

//----------------------------------------------------------------------------
// EXPORTS

module.exports = {

// Information on what's running us. This is very easy to get.

'Node': {
	'Node.js version': process.version,
	'Node.js path': process.execPath,
	'Fourdit path': process.argv[1]
},

'Node Modules': function() {

	// Get the versions of the node_modules we're using. Just scan the
	// node_modules directory and look at the package.json files. Note that
	// this is a self-executing function which returns an object.

	var mod_vers = {},	// object to return
		mod_desc,		// shorthand, points to package.json
		mod_info,		// used to hold the package.json info
		i;				// loop counter

	// I'll do all this synchronously. It'll virtually never be run, and
	// it's scanning a very small directory.

	var mod_dirs = fs.readdirSync('./node_modules');

	for (i = 0; i < mod_dirs.length; i++) {
		mod_desc = path.resolve(path.join('node_modules', mod_dirs[i],
		'package.json'));

		if (fs.existsSync(mod_desc)) {
			mod_info = JSON.parse(fs.readFileSync(mod_desc, 'utf8'));
			mod_vers[mod_dirs[i]] = mod_info.version + ' (' +
			path.dirname(mod_desc) + ')';
		}

	}

	return mod_vers;
}(),

// Information on the SQLite database file

'Database': {
	'Database file': path.resolve(config.DB),
	'Database size': (fs.statSync(config.DB).size / 1024) + 'kb'
},

// Platform information. Probably irrelevant, but free.

'Operating System': {
	'OS Platform': os.platform(),
	'OS Release': os.release()
}

}
